/**
 * @fileOverview hover style
 * @author huangtonger@aliyun.com
 */

const Util = require('../util');
const Mixin = {};

Mixin.AUGMENT = {
  addOutterShape(item, style) {
    this.clearOutterShape(item);
    const { lineWidth } = style;
    const keyShape = item.getKeyShape();
    const group = item.getGraphicGroup();
    const originAttrs = keyShape.attr();
    const type = keyShape.get('type');
    const originLineWidth = keyShape.attr('lineWidth');
    const outterShapeAttrs = Util.clone(originAttrs);
    // remove un need attrs
    delete outterShapeAttrs.fillStyle;
    delete outterShapeAttrs.strokeStyle;
    delete outterShapeAttrs.matrix;
    const outterShape = group.addShape(type, {
      attrs: {
        ...outterShapeAttrs,
        fill: null,
        ...style
      }
    });
    Util.toBack(outterShape, group);
    const box = outterShape.getBBox();
    const width = box.maxX - box.minX;
    const height = box.maxY - box.minY;
    const centerX = (box.minX + box.maxX) / 2;
    const centerY = (box.minY + box.maxY) / 2;
    outterShape.transform([
      [ 't', -centerX, -centerY ],
      [ 's', (lineWidth + width + originLineWidth) / width, (lineWidth + height + originLineWidth) / height ],
      [ 't', centerX, centerY ]
    ]);
    outterShape.isOutter = true;
    item.outterShape = outterShape;
  },
  clearOutterShape(item) {
    if (item.outterShape) {
      item.outterShape.remove();
    }
  }
};

module.exports = Mixin;
